﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Media;

namespace nl.tno.ict.multitouch.wpfmultitouch.controls
{
    public class SimpleCircleAdorner : Adorner
    {
        #region constructor

        /// <summary>
        /// Be sure to call the base class constructor.
        /// </summary>
        /// <param name="adornedElement">Base element</param>
        public SimpleCircleAdorner(UIElement adornedElement)
            : base(adornedElement)
        {
            this.PreviewMouseDown += new System.Windows.Input.MouseButtonEventHandler(SimpleCircleAdorner_PreviewMouseDown);
            this.PreviewMouseMove += new System.Windows.Input.MouseEventHandler(SimpleCircleAdorner_PreviewMouseMove);
            this.PreviewMouseUp += new System.Windows.Input.MouseButtonEventHandler(SimpleCircleAdorner_PreviewMouseUp);
        }

        void SimpleCircleAdorner_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (((FrameworkElement)AdornedElement).Parent is MTCanvas)
                ((MTCanvas)((FrameworkElement)AdornedElement).Parent).MTCanvas_PreviewMouseLeftButtonUp(sender, e);
            else if (((FrameworkElement)AdornedElement).Parent is MTGrid)
                ((MTGrid)((FrameworkElement)AdornedElement).Parent).MTGrid_PreviewMouseLeftButtonUp(sender, e);
        }

        void SimpleCircleAdorner_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (((FrameworkElement)AdornedElement).Parent is MTCanvas)
                ((MTCanvas)((FrameworkElement)AdornedElement).Parent).MTCanvas_PreviewMouseMove(sender, e);
            else if (((FrameworkElement)AdornedElement).Parent is MTGrid)
                ((MTGrid)((FrameworkElement)AdornedElement).Parent).MTGrid_PreviewMouseMove(sender, e);
        }

        void SimpleCircleAdorner_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (((FrameworkElement)AdornedElement).Parent is MTCanvas)
                ((MTCanvas)((FrameworkElement)AdornedElement).Parent).MTCanvas_PreviewMouseLeftButtonDown(sender, e);
            else if (((FrameworkElement)AdornedElement).Parent is MTGrid)
                ((MTGrid)((FrameworkElement)AdornedElement).Parent).MTGrid_PreviewMouseLeftButtonDown(sender, e);            
        }

        #endregion

        /// <summary>
        /// A common way to implement an adorner's rendering behavior is to override the OnRender
        /// method, which is called by the layout system as part of a rendering pass.
        /// </summary>
        /// <param name="drawingContext">The context to be drawn</param>
        protected override void OnRender(DrawingContext drawingContext)
        {
            Rect adornedElementRect = new Rect(new Size(((FrameworkElement)this.AdornedElement).ActualWidth, ((FrameworkElement)this.AdornedElement).ActualHeight));

            // Some arbitrary drawing implements.
            SolidColorBrush renderBrush = new SolidColorBrush(Colors.LightBlue);
            //renderBrush.Opacity = 0.4;
            Pen renderPen = new Pen(new SolidColorBrush(Colors.DarkBlue), 1.5);
            double renderRadius = 10.0;

            // Draw a circle at each corner.
            drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopLeft, renderRadius, renderRadius);
            drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopRight, renderRadius, renderRadius);
            drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomLeft, renderRadius, renderRadius);
            drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomRight, renderRadius, renderRadius);

        }
    }

}
